<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\model\Cangku;

/**
 * 话费接口
 */
class Huafei extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
    } 
     /**
     * 话费记录 status状态：1=收入，2=支出
     */
    public function list(){
      $return = Db::name('huafei_log')->where('user_id',$this->user_id)->field("*,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->order('id desc')->select();
      $this->success('话费记录',$return);
    } 
 
    /**
     * 话费充值
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/huafei/huafei_chongzhi)
     * @ApiParams   (name="huafei_price",type=decimal,required=true,description="话费值")
     * @ApiReturn({

     })
     */
    public function huafei_chongzhi(){
      $huafei_price = input('huafei_price');

      $my_huafei_price = $this->auth->huafei_price;//个人话费

      if($my_huafei_price<$huafei_price)$this->error('话费余额不足已兑换');
      if($huafei_price<10)$this->error('至少10元起兑换');

      $insert_huafei_log['text'] = '话费兑换'.$huafei_price;
      $insert_huafei_log['user_id'] = $this->user_id;
      $insert_huafei_log['createtime'] = time();
      $insert_huafei_log['huafei_price'] = $huafei_price;
      $insert_huafei_log['status'] = 2;

      Db::name('huafei_log')->insert($insert_huafei_log);//话费记录
      Db::name('user')->where('id',$this->user_id)->setDec('huafei_price',$huafei_price);
      $this->success('兑换成功');
    } 

  

  

}
